/**
 * 功能描述
 */
define(function (require, exports, module) {
	var HandlerBars = require("handlebars");
	var cartItemsTpl = require("../assets/tpl/cart_items.tpl");
	var orderConfirmTpl = require("../assets/tpl/order_confirm.tpl");
	
    module.exports = {
    	addCart:function(data,widget){
    		var cartData = localStorage.getItem("cartData");
   			cartData = cartData?JSON.parse(cartData):{};	
    		
			if(cartData[data.type+"_"+data.pk]){
				cartData[data.type+"_"+data.pk].num++;
			}else{
				var tdata = null;
	    		if(data.type == "service"){
	    			var serviceItemData = widget.get("serviceItemData");
	    			tdata = $.grep( serviceItemData, function(n,i){
	    				  if(n.pk == data.pk){
	    					  return n;
	    				  }
	    				});
	    		}else if(data.type == "goods"){
	    			var goodsItemData = widget.get("goodsItemData");
	    			tdata = $.grep( goodsItemData, function(n,i){
	  				  if(n.pk == data.pk){
	  					  return n;
	  				  }
	  				});
	    		}else if(data.type == "dish"){
	    			var dishItemData = widget.get("dishItemData");
	    			tdata = $.grep( dishItemData, function(n,i){
	  				  if(n.pk == data.pk){
	  					  return n;
	  				  }
	  				});
	    		}
				cartData[tdata[0].type+"_"+tdata[0].pk] = tdata[0];
				cartData[tdata[0].type+"_"+tdata[0].pk].num = 1;
			}
    		
    		localStorage.setItem("cartData",JSON.stringify(cartData));
    		this.setTotalPrice(widget,cartData);
    		this.updataCartDom(widget,cartData);
    	},
    	decreaseCart:function(data,widget){
    		var cartData = localStorage.getItem("cartData");
    		if(cartData){
    			cartData = JSON.parse(cartData);	
    		}else{
    			cartData = {};
    		}
    		
			if(cartData[data.type+"_"+data.pk].num == 1){
				delete cartData[data.type+"_"+data.pk];
			}else{
				cartData[data.type+"_"+data.pk].num--;
			}
    		
    		localStorage.setItem("cartData",JSON.stringify(cartData));
    		this.setTotalPrice(widget,cartData);
    		this.updataCartDom(widget,cartData);
    	},
    	removeItem:function(data,widget){
    		var cartData = localStorage.getItem("cartData");
    		if(cartData){
    			cartData = JSON.parse(cartData);	
    		}else{
    			cartData = {};
    		}
    		
    		delete cartData[data.type+"_"+data.pk];
    		
    		localStorage.setItem("cartData",JSON.stringify(cartData));
    		this.setTotalPrice(widget,cartData);
    		this.updataCartDom(widget,cartData);
    		
    	},
    	removeAll:function(widget){
    		localStorage.setItem("cartData","");
    		this.setTotalPrice(widget);
    		this.updataCartDom(widget);
    	},
    	setTotalPrice:function(widget,cartData){
    		var total = 0;
    		for(var key in cartData){
    			var one = parseFloat(cartData[key].price * cartData[key].num);
    			total += one;
    		}
    		
    		if(total == 0){
    			$("#cart").css('background-image','url("assets/eling/elcms/pos/productitem/src/assets/img/shoppingcart_icon.png")');
    			$("#settlement").addClass("disabled");
    			$("#goSettlement").addClass("disabled");
    		}else{
    			$("#cart").css('background-image','url("assets/eling/elcms/pos/productitem/src/assets/img/shoppingcart_content_icon.png")');
    			$("#settlement").removeClass("disabled");
    			$("#goSettlement").removeClass("disabled");
    		}
    		
    		$("#total-price").html("￥"+total.toFixed(2));
    		$("#totalAmount").html(total.toFixed(2));
    	},
    	updataCartDom: function(widget,cartData){
        	var tdata = [];
        	for(var key in cartData){
        		tdata.push(cartData[key]);
        	}
        	var html = HandlerBars.compile(cartItemsTpl)({itemgruop:tdata});
        	$("#shoopingCartItem").html(html);
        	
        	if(!$("#order-confirm-center").hasClass("hidden")){
        		this.updateOrderConfirmDom(widget,cartData);
        	}
        },
        updateOrderConfirmDom:function(widget,cartData){
    		var typegruop={};
    		for(var key in cartData){
    			if(typegruop[cartData[key].type+"_type_"+cartData[key].parent.pk]){
    				typegruop[cartData[key].type+"_type_"+cartData[key].parent.pk].data.push(cartData[key]);
    			}else{
    				typegruop[cartData[key].type+"_type_"+cartData[key].parent.pk] = {
    						pk:cartData[key].parent.pk,
    						name:cartData[key].parent.name,
    						data:[]
    				};
    				typegruop[cartData[key].type+"_type_"+cartData[key].parent.pk].data.push(cartData[key]);
    			}
    		}
    		var sdata = [];
    		for(var key in typegruop){
    			sdata.push(typegruop[key]);
    		}
    		
    		var html = HandlerBars.compile(orderConfirmTpl)(sdata);
			$("#order-confirm-center").html(html);
        }
    };
});
